function [imgFV, bpfa] = fvLearnBPFA( img, rf, params)

img = double(img);
[H,W,Band] = size(img);

if max(max(max(img))) > 1 || min(min(min(img))) <0
    disp('Input image must scale to 0~1')
    return
end

   numTrain = 10000;
   opts.K = 300;
   opts.numepochs =   1;
   opts.batchsize = 500;

   if nargin==3
    numTrain = params.numTrain-mod(params.numTrain,500)
    opts.K = params.K;
    opts.maxIters =   params.numIters;
    opts.batchsize = 500;
    else if nargin>3
            disp('Input parameters wong');
            return;
        end
   end
   
   fprintf('Sample train_x begin\n');
   train_x = SamplePatches(img, rf,numTrain); % NxD
   whos train_x
   fprintf('Sample train_x finished\n');
   
   [bpfa, VBparam] = BPFA( train_x', opts.K, opts.maxIters );
   
   fvDim = bpfa.K;
   imgFV = zeros(H,W,fvDim); 

    tmpImg = Img2PImg(img, rf);

    for i=1:H
        tmpData = squeeze( tmpImg(i,:,:) ) ;
        tmpFV = UpBPFA(tmpData', bpfa);  % input data: DxN, output: KxN
        imgFV(i,:,:) = tmpFV';
    end
   

end
